package data_structure;

public class leetcode_0654 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums,0,nums.length-1);
    }
    public TreeNode build(int[] nums,int left,int right){
        if(right-left<0) return null;
        if(right-left==0) return new TreeNode(nums[right]);
        int rootindex=0;
        int max=-1;
        for(int i=left;i<=right;++i){
            if(nums[i]>max){
                max=nums[i];
                rootindex=i;
            }
        }
        TreeNode root=new TreeNode(nums[rootindex]);
        root.left=build(nums,left,rootindex-1);
        root.right=build(nums,rootindex+1,right);
        return root;
    }
}
